package com.tju.backend.plugin.web_socket;

import org.tio.server.ServerTioConfig;
import org.tio.websocket.server.WsServerStarter;

import java.io.IOException;

/**
 * WebSocket配置
 *
 * @USER: CLS
 */
public class TioWebsocketStarter {

    private final WsServerStarter wsServerStarter;

    public TioWebsocketStarter(int port, TioMsgHandler wsMsgHandler) throws Exception {
        wsServerStarter = new WsServerStarter(port, wsMsgHandler);
        ServerTioConfig serverTioConfig = wsServerStarter.getServerTioConfig();
        serverTioConfig.setName(TioServerConfig.PROTOCOL_NAME);
        serverTioConfig.setServerAioListener(TioServerAioListener.me);

        //设置ip监控
        serverTioConfig.setIpStatListener(TioStatListener.me);

        //设置ip统计时间段
        serverTioConfig.ipStats.addDurations(TioServerConfig.IpStatDuration.IPSTAT_DURATIONS);

        //设置心跳超时时间
        serverTioConfig.setHeartbeatTimeout(TioServerConfig.HEARTBEAT_TIMEOUT);

    }

    /**
     * @throws IOException
     * @author cls
     */
    public static void start() throws Exception {
        TioWebsocketStarter appStarter = new TioWebsocketStarter(TioServerConfig.SERVER_PORT, TioMsgHandler.me);
        appStarter.wsServerStarter.start();
    }
}
